﻿function getQueryString(name) {
    var svalue  = location.href.match(new RegExp("[\?\&]" + name + "=([^\&]*)(\&?)","i"));
    return svalue ? svalue[1] : svalue;
}
angular.module('stamain.controller', [])
  .controller('stamainCtrl', ['$scope', '$timeout', '$localstorage', '$ionicPopup', 'stamainService', '$state', 'loginService', '$ionicHistory', function ($scope, $timeout, $localstorage, $ionicPopup, stamainService, $state, loginService, $ionicHistory) {

      $scope.params = {};
      $scope.userlist = [];
      $scope.params = stamainService.getParams();
      $scope.visible = true; //初始
      $scope.$on('$ionicView.enter', function (event, data) {
          console.log("报表统计页面进来了");
          $scope.params.Depart_Code = $localstorage.get("Depart_Code");
          $scope.params.User_ID = $localstorage.get("User_ID");
          if ($scope.userlist.length <= 0) {
              $scope.doRefresh( );
          }
      });

      $scope.doRefresh = function () {
          console.log("doRefresh刷新数据");
          stamainService.get_StamainInfo($scope.params).then(function (data) {
              $scope.initHighcharts('sta1', data.main);
              $scope.userlist = data.list;  
              var i=0;
              for (i = 0; i < data.list.length; i++) {
                  $scope.initUserChart(data.list[i]);
              }
              $("#userlist text").each(function (i) {
                  if ($(this).html().indexOf('Highcharts.com') >= 0) {
                      $(this).css("display", "none");
                  }
              });

          }, function (error) {
              var alertPopup = $ionicPopup.alert({
                  title: '获取下级成员失败！',
                  template: "网络异常",
                  okText: '确定', 
                  okType: 'item-customer-bg-darkred',  
              });
          });
          
      }
      $scope.initHighcharts = function (id, _data) {
          var numdata=[ 
                ['未呼叫', _data.callstate0],
                ['未标记', _data.followState0],
                ['无意向', _data.followState1],
                ['待跟进', _data.followState2],
                ['已成交', _data.followState3]];
          console.log("numdata:" + angular.toJson(numdata));
          $('#' + id).highcharts({
              chart: {
                  plotBackgroundColor: null,
                  plotBorderWidth: null,
                  plotShadow: false
              },
              title: {
                  text: ''
              },
              tooltip: {
                  headerFormat: '{series.name}<br>',
                  pointFormat: '{point.name}: <b>{point.percentage:.1f}%</b>'
              },
              plotOptions: {
                  pie: {
                      allowPointSelect: true,
                      cursor: 'pointer',
                      dataLabels: {
                          enabled: false
                      },
                      showInLegend: true
                  }
              },
              series: [{
                  type: 'pie',
                  name: '占比',
                  data: numdata
              }]  
          });
      }

      $scope.initUserChart = function (data) {
          $('#userlist').append('<div class="item  "   >  <div id="M_' + data.User_ID + '" style="height:200px"> </div> </div>');

          $('#M_' + data.User_ID).highcharts({
              chart: {
                  type: 'column'
              },
              title: {
                  text: data.User_Name
              },
              subtitle: {
                  text: data.Phone
              },
              xAxis: {
                  categories: [
                      '总数',
                      '未呼叫',
                      '已呼叫',
                      '未标记',
                      '无意向',
                      '待跟进',
                      '已成交'
                  ],
                  crosshair: true
              },
              yAxis: {
                  min: 0,
                  title: {
                      text: ''
                  }
              }, 
              plotOptions: {
                  column: {
                      borderWidth: 0
                  }
              },
              series: [{
                  name: '完成量',
                  data: [data.count, data.callstate0, data.callstate1, data.followState0, data.followState1, data.followState2, data.followState3]
              } ]
          });
      }
      
      $scope.goBack = function () {
          $ionicHistory.goBack(-1);//back到之前的路由
      }
  }]);
